package database;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

/**
 * class DbFileManager - provide API of queries of the files table
 */
public class DbFileManager {
	
	private Session session;
	
	/**
	 * constructor
	 * @param session - a session instance
	 */
	public DbFileManager(Session session) {
		this.session = session;
	}
	
	/**
	 * save file in files table in the DB
	 * @param file - a file to add to DB.
	 * @return 1 - if succeed. 0 - if failed.
	 */
	public synchronized int saveDbFile(DbFile file){
		
		DbFile existFile = getDbFileHelper(file.getFileName(),file.getFolderID());
		//mean it is already there
		if (existFile!=null)
		{
			if (existFile.getFileID().equals(file.getFileID()))
			{
				System.out.println("file exist = same fileName & folderName & check fileId..");
				return 0;
			}
			else
			{
				existFile.setFileID(file.getFileID());
				session.update(existFile);
				session.flush();
				session.clear();
				return 1;
				
			}
		}
		
		//System.out.println("save file");
		session.save(file);
		session.flush();
		session.clear();
		return 1;
	}
	

	//return the file record in the DB according to file name and filder id
	private DbFile getDbFileHelper(String fileName, Integer folderID){
		Query query = session.createQuery("from DbFile");
		
		List<DbFile> list = query.list();
		Iterator<DbFile> it=list.iterator();
		
		DbFile currentFile= null;
		
		while (it.hasNext()){
			currentFile=it.next();
			
			if ((fileName.equals(currentFile.getFileName())) && (folderID.equals(currentFile.getFolderID())))
				return currentFile;
		}
		
		return null;
	}
	
	/**
	 * @param fileName - file name
	 * @param folderID - folder id that the file is under it
	 * @return the file from the DB
	 */
	public synchronized DbFile getDbFile(String fileName, Integer folderID){
		return getDbFileHelper(fileName, folderID);
	}
	
	
	/**
	 * update the file in the DB
	 * @param file - a file
	 */
	public synchronized void updateDbFile(DbFile file){
		session.update(file);
		session.flush();
		session.clear();
	}
	
	/**
	 * delete the file from the DB
	 * @param file - a file
	 */
	public synchronized void deleteDbFile(DbFile file) {
		session.delete(file);
		session.flush();
		session.clear();
	}
	
	/**
	 * delete files that under the folder folderID
	 * @param folderID - a folder id
	 */
	public synchronized void deleteFilesInFolder(Integer folderID)
	{
		System.out.println("delete query");
		String queryString = "delete from DbFile where folderID='" +folderID +"'";
		Query query = session.createQuery(queryString);
		query.executeUpdate();		
	}
	
	/**
	 * @param folderID - a folder id
	 * @return a list of file that under that folder
	 */
	public List<DbFile> getFilesFromFolderID(Integer folderID)
	{
		String queryString = "from DbFile where folderID='"+folderID+"' order by fileName";
		Query query = session.createQuery(queryString);
		
		return query.list();
	}


}
